package com.tungse.study.io.two;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class Server2 {

    public static void main(String[] args) {
        try {
            System.out.println("服务端启动");
            //1 定义一个serverSocket对象进行服务端的端口注册
            ServerSocket serverSocket = new ServerSocket(9999);
            //2 监听客户端的socket链接请求
            Socket socket = serverSocket.accept();
            //3 从socket中获取输入流，读取客户端发送的数据
            InputStream is = socket.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
            String msg = null;
            while ((msg = bufferedReader.readLine()) != null) {
                System.out.println("服务端接收到客户端发送的消息：" + msg);
            }



        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }
}
